/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package df.bean.interfacefile;
import df.bean.db.conn.DBConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import df.bean.db.conn.DBConn;
import df.bean.obj.util.JDate;
import df.bean.obj.util.JNumber;
public class ExportDFToGLBean extends InterfaceTextFileBean{
    private ResultSet rs;
    private Statement stm;
    private DBConn dcon;
    private String payment_date = "";
    private String transaction_date = "";

    public void setPaymentDate(String s){
    	this.payment_date = s;
    }
    public void setTransactionDate(String s){
    	this.transaction_date = s;
    }

    @Override
    public boolean insertData(String fn, DBConnection d) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    private String ship(String text, char c, int lengthStr, String positon){
        int lengthText = text.length();
        if(positon==null) positon = "LEFT";
        if(lengthText==lengthStr){
            return text;
        }else if(lengthText > lengthStr){
            return "Error";
        }else{
            String str = "";
            int dif = lengthStr - lengthText;
            for(int i = 0; i < dif ; i++){
                str+= c + "";
            }
            if("LEFT".equalsIgnoreCase(positon)){
                return text+str;
            }else{
                return str+text;
            }
        }
    }

    @Override
    public boolean exportData(String fn, String hp, String type, String year, String month, DBConn dInMethod, String path) {
        boolean status = true;
        DBConnection conn = new DBConnection();
        conn.connectToLocal();
        
        String[] data_export = null;
        String data = "select HOSPITAL_CODE, SEQ_NO, ACCOUNTING_DT, ACCOUNTING_TIME, TYPE, " +
                "ACCOUNT, DEPT_ID, PRODUCT, MONETARY_AMOUNT, CLASS_FLD, CHARTFIELD2, CHARTFIELD3, " +
                "CASE WHEN (AMOUNT_SIGN = '*' OR AMOUNT_SIGN = '+') THEN '+' ELSE '-' END AS AMOUNT_SIGN " +//NEW
                //"CHARTFIELD2, CHARTFIELD3, AMOUNT_SIGN " +
                "from INT_ERP_GL " +
                "where YYYY = '"+year+"' and MM='"+month+"' and TYPE='"+type+"';";
        String countRowSQL = "select count(*) " +
                "from INT_ERP_GL " +
                "where YYYY = '"+year+"' and MM='"+month+"' AND HOSPITAL_CODE = '"+hp+"' and TYPE='"+type+"';";
        try {
            setFileName(path);
            //temp_data = dInMethod.query(data);

            int countRow = Integer.parseInt(conn.executeQueryString(countRowSQL));
            rs = conn.executeQuery(data);
            data_export = new String[countRow];
            int iCount = 0;
            while(rs.next()){
                data_export[iCount] = ""
                        + ship(rs.getString("HOSPITAL_CODE"),' ',5,"LEFT")
                        + ship(rs.getString("SEQ_NO"),'0',9,"RIGHT")
                        + ship(rs.getString("ACCOUNTING_DT"),' ',10,"LEFT")
                        //+ ship("DOCTORFEE",' ',10,"LEFT")
                        + ship("FRONTEND",' ',10,"LEFT")
                        + ship(rs.getString("ACCOUNT"),' ',6,"LEFT")
                        + ship(rs.getString("DEPT_ID"),' ',10,"LEFT")
                        + ship(rs.getString("PRODUCT"),' ',6,"LEFT")
                        + ship("",' ',15,"LEFT")
                        + ship("THB",' ',3,"LEFT")
                        + ship("",' ',3,"LEFT")
                        + ship("000000000000000",' ',15,"LEFT")
                        + ship(rs.getString("AMOUNT_SIGN"),' ',1,"RIGHT")
                        + ship(rs.getString("MONETARY_AMOUNT").replaceAll("\\.", ""),'0',14,"RIGHT")
                        + ship("",' ',8,"LEFT")
                        + ship(rs.getString("CLASS_FLD"),' ',5,"LEFT")
                        + ship("",' ',10,"LEFT")
                        + ship(rs.getString("CHARTFIELD2"),' ',10,"LEFT")
                        + ship(rs.getString("CHARTFIELD3"),' ',10,"LEFT")
                        + ship("",' ',5,"LEFT")
                        + ship("",' ',5,"LEFT");
                iCount++;
            }
        }catch(Exception e){
            System.out.println(e);
        }

        writeFileNew(data_export);
        return status;
    }
    
    public boolean exportDataVTN(String fn, String hp, String type, String year, String month, DBConn dInMethod, String path) {
        boolean status = true;
        DBConnection conn = new DBConnection();
        conn.connectToLocal();
        DBConn dconn = new DBConn();
        String[] data_export = null;
        String q_gl_script = "";
        if(type.equals("AC")){
        	q_gl_script = 
            	"SELECT HOSPITAL_CODE, ACCOUNTING_DATE, ACCOUNT_CODE, FIRST_DEPARTMENT_CODE, "+
            	"FIRST_LOCATION_CODE, PATIENT_LOCATION_CODE, RECEIPT_LOCATION_CODE, "+
            	"CASE WHEN ADMISSION_TYPE_CODE = 'I' THEN 'IPD' ELSE 'OPD' END AS ADMISSION_TYPE_CODE, " +
            	"ACTIVITY, NATIONALITY_CODE, CURRENCY_CODE, " +
            	//"AMOUNT_SIGN, " +
            	"CASE WHEN AMOUNT_SIGN = '+' THEN '' ELSE AMOUNT_SIGN END AS AMOUNT_SIGN, "+
            	"SUM(AMOUNT_AFT_DISCOUNT) AS AMOUNT, ACCOUNTING_TEMPLATE " +
            	"FROM TEMP_GL "+
            	"WHERE ACCOUNTING_TEMPLATE = 'DFACCRUE' "+
            	"AND YYYY = '"+year+"' AND MM='"+month+"' "+
            	"GROUP BY HOSPITAL_CODE, ACCOUNTING_DATE, ACCOUNT_CODE, FIRST_DEPARTMENT_CODE, "+
            	"FIRST_LOCATION_CODE, PATIENT_LOCATION_CODE, RECEIPT_LOCATION_CODE, "+
            	"ADMISSION_TYPE_CODE, ACTIVITY, NATIONALITY_CODE, CURRENCY_CODE, AMOUNT_SIGN, "+
            	"ACCOUNTING_TEMPLATE";
        }else{ //type = GL
        	q_gl_script = 
            	"SELECT HOSPITAL_CODE, ACCOUNTING_DATE, ACCOUNT_CODE, FIRST_DEPARTMENT_CODE, "+
            	"FIRST_LOCATION_CODE, PATIENT_LOCATION_CODE, RECEIPT_LOCATION_CODE, "+
            	"CASE WHEN ADMISSION_TYPE_CODE = 'I' THEN 'IPD' ELSE 'OPD' END AS ADMISSION_TYPE_CODE, " +
            	"ACTIVITY, NATIONALITY_CODE, CURRENCY_CODE, " +
            	//"AMOUNT_SIGN, " +
            	"CASE WHEN AMOUNT_SIGN = '+' THEN '' ELSE AMOUNT_SIGN END AS AMOUNT_SIGN, "+
            	"SUM(AMOUNT_AFT_DISCOUNT) AS AMOUNT, ACCOUNTING_TEMPLATE " +
            	"FROM TEMP_GL "+
            	"WHERE ACCOUNTING_TEMPLATE <> 'DFACCRUE' "+
            	"AND YYYY = '"+year+"' AND MM='"+month+"' "+
            	"GROUP BY HOSPITAL_CODE, ACCOUNTING_DATE, ACCOUNT_CODE, FIRST_DEPARTMENT_CODE, "+
            	"FIRST_LOCATION_CODE, PATIENT_LOCATION_CODE, RECEIPT_LOCATION_CODE, "+
            	"ADMISSION_TYPE_CODE, ACTIVITY, NATIONALITY_CODE, CURRENCY_CODE, AMOUNT_SIGN, "+
            	"ACCOUNTING_TEMPLATE";
        }
        	
        try {
            setFileName(path);
            try {
    			dconn.setStatement();
    		} catch (SQLException e1) {
    		}
            String[][] tb = dconn.query(q_gl_script);  
            String keep_double = "";
            String[] temp = null;
            int countRow = tb.length;
            //System.out.println("Number of row "+countRow);
            rs = conn.executeQuery(q_gl_script);
            //rs.beforeFirst();//new
            data_export = new String[countRow];
            int iCount = 0;
            while(rs.next()){
            	keep_double = ""+rs.getDouble("AMOUNT");
            	temp = keep_double.split("[.]");
            	if(temp.length>1){
                	keep_double = temp[1];
                	if(keep_double.length()<2){
                		temp[1] = temp[1]+"0";
                	}
                	keep_double = temp[0]+"."+temp[1];
            	}else{
            	}
                data_export[iCount] = ""+
                		rs.getString("HOSPITAL_CODE").toString()+"|"+
                		iCount+"|"+
                		this.transaction_date+"|"+
                		//JDate.showDate(rs.getString("ACCOUNTING_DATE").toString())+"|"+
                		rs.getString("ACCOUNT_CODE").toString()+"|"+
                		rs.getString("FIRST_DEPARTMENT_CODE").toString()+"|"+
                		rs.getString("FIRST_LOCATION_CODE").toString()+"|"+
                		rs.getString("PATIENT_LOCATION_CODE").toString()+"|"+
                		rs.getString("RECEIPT_LOCATION_CODE").toString()+"|"+
                		rs.getString("ADMISSION_TYPE_CODE").toString()+"|"+
                		"|"+ //set Activity = blank
                		//rs.getString("ACTIVITY").toString()+"|"+
                		rs.getString("NATIONALITY_CODE").toString()+"|"+
                		rs.getString("CURRENCY_CODE").toString()+"|"+
                		rs.getString("AMOUNT_SIGN").toString()+
                		keep_double+"|"+
                		//rs.getDouble("AMOUNT")+"|"+
                		rs.getString("ACCOUNTING_TEMPLATE").toString();
                //System.out.println(data_export[iCount]);
                iCount++;
            }
        }catch(Exception e){
            System.out.println("exportDataVTN : "+e);
        }finally{
        	try{rs.close();}catch (Exception e){}
        }
        writeFileNew(data_export);
        return status;
    }    
}
